﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.ORM;

namespace XDesigner.AppFramework.DOM
{
    [ORMType("Table_User")]
    public class AppUser
    {
        public AppUser()
        {
        }

        private int _RecordID = 0;
        [ORMKeyField()]
        public virtual int RecordID
        {
            get { return _RecordID; }
            set { _RecordID = value; }
        }

        private string _Name = null;

        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Description = null;

        public virtual string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        private string _Password = null;
        [ORMDelay()]
        public virtual string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        private bool _Enabled = true;
        [System.ComponentModel.DefaultValue( true )]
        public virtual bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

        private AppDepartment _Department = null;
        [ORMRelation( ORMRelationStyle.OneToOne )]
        [ORMField("DepartmentID")]
        [ORMDelay()]
        public virtual AppDepartment Department
        {
            get { return _Department; }
            set { _Department = value; }
        }

        private List<AppRole> _Roles = null;
        [ORMDelay()]
        [ORMRelation( ORMRelationStyle.OneToManyWithKeyList )]
        [ORMField("Roles")]
        public virtual List<AppRole> Roles
        {
            get { return _Roles; }
            set { _Roles = value; }
        }

        public bool CheckPermission(string name)
        {
            if (this.Roles != null)
            {
                foreach (AppRole role in this.Roles)
                {
                    if (role.Permissions != null)
                    {
                        foreach (AppPermission p in role.Permissions)
                        {
                            if (string.Compare(p.Name, name, true) == 0)
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

    }

}
